package com.mobile.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.mobile.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.File;

/**
 * @author Encounter
 * @date 2024/10/11 10:48<br/>
 */
@RestController("clientSetmealController")
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController
    {
        @Autowired
        private SetmealService setmealService;
        
        /**
         * 获取全部 Setmeal 不分页
         *
         * @return {@link Result }
         */
        @GetMapping("/getSetmeal")
        public Result getSetmeal(Model model)
            {
                try
                    {
                        log.info("获取全部套餐");
                        String filePath = setmealService.queryAll(model);
                        if (new File(filePath).exists())
                            {
                                //文件存在，返回文件路径
                                return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS, filePath);
                            }
                        else
                            {
                                return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
                            }
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
                    }
            }
        
        /**
         * 根据套餐id查询套餐详情，包含（套餐基本信息、套餐关联的检查组、检查组关联的检查项）
         *
         * @param id 套餐id
         * @return {@link Result }
         */
        @PostMapping("/findById")
        public Result findById(@RequestParam Integer id, Model model)
            {
                try
                    {
                        log.info("要查询的id：{}", id);
                        String filePath = setmealService.findDetailById(id, model);
                        if (new File(filePath).exists())
                            {
                                //文件存在，返回文件路径
                                return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, filePath);
                            }
                        else
                            {
                                return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
                            }
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
                    }
            }
    }
